<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible"
  >
    <el-form
      :model="dataForm"
      :rules="dataRule"
      ref="dataForm"
      @keyup.enter.native="dataFormSubmit()"
      label-width="80px"
    >
      <el-form-item label="考试名称" prop="examName">
        <el-input v-model="dataForm.examName" placeholder="考试名称"></el-input>
      </el-form-item>
      <el-form-item label="题目数量" prop="examCount">
        <el-input
          v-model="dataForm.examCount"
          placeholder="题目数量"
        ></el-input>
      </el-form-item>
      <el-form-item label="考试总分" prop="examSum">
        <el-input v-model="dataForm.examSum" placeholder="考试总分"></el-input>
      </el-form-item>
      <el-form-item label="考试封面" prop="examHover">
        <el-input
          v-model="dataForm.examHover"
          placeholder="考试封面"
        ></el-input>
      </el-form-item>
      <el-form-item label="发布讲师" prop="teacherName">
        <el-input
          v-model="dataForm.teacherName"
          placeholder="发布讲师"
        ></el-input>
      </el-form-item>



      <el-form-item label="开始时间" prop="startTime">
        <el-date-picker
          v-model="dataForm.startTime"
          type="datetime"
          placeholder="选择日期"
          @change="calculateExamTime"
        >
        </el-date-picker>
      </el-form-item>

      <el-form-item label="结束时间" prop="endTime">
        <el-date-picker
          v-model="dataForm.endTime"
          type="datetime"
          placeholder="选择日期"
          @change="calculateExamTime"
        >
        </el-date-picker>
      </el-form-item>

      <el-form-item label="考试时间" prop="examTime">
        <el-input v-model="dataForm.examTime" placeholder="考试时间"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>

export default {
  data () {
    return {
      visible: false,
      dataForm: {
        id: 0,
        examName: '',
        examCount: '',
        examSum: '',
        examHover: '',
        teacherName: '',
        startTime: '',
        endTime: '',
        examTime: '',
        createTime: '',
        updateTime: '',
        isDelete: ''
      },
      dataRule: {
        examName: [
          { required: true, message: '考试名称不能为空', trigger: 'blur' }
        ],
        examCount: [
          { required: true, message: '题目数量不能为空', trigger: 'blur' }
        ],
        examSum: [
          { required: true, message: '考试总分不能为空', trigger: 'blur' }
        ],
        examHover: [
          { required: true, message: '考试封面不能为空', trigger: 'blur' }
        ],
        teacherName: [
          { required: true, message: '发布讲师不能为空', trigger: 'blur' }
        ],
        startTime: [
          { required: true, message: '开始时间不能为空', trigger: 'blur' }
        ],
        endTime: [
          { required: true, message: '结束时间不能为空', trigger: 'blur' }
        ],
        examTime: [
          { required: true, message: '考试时间不能为空', trigger: 'blur' }
        ],
        createTime: [
          { required: false, message: '创建时间不能为空', trigger: 'blur' }
        ],
        updateTime: [
          { required: false, message: '修改时间不能为空', trigger: 'blur' }
        ]
      }
    };
  },
  methods: {
    init (id) {
      this.dataForm.id = id || 0;
      this.visible = true;
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields();
        if (this.dataForm.id) {
          this.$http({
            url: this.$http.adornUrl(`/exam/exam/info/${this.dataForm.id}`),
            method: 'get',
            params: this.$http.adornParams()
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.dataForm.examName = data.exam.examName;
              this.dataForm.examCount = data.exam.examCount;
              this.dataForm.examSum = data.exam.examSum;
              this.dataForm.examHover = data.exam.examHover;
              this.dataForm.teacherName = data.exam.teacherName;
              this.dataForm.startTime = data.exam.startTime;
              this.dataForm.endTime = data.exam.endTime;
              this.dataForm.examTime = data.exam.examTime;
              this.dataForm.createTime = data.exam.createTime;
              this.dataForm.updateTime = data.exam.updateTime;
              this.dataForm.isDelete = data.exam.isDelete;
            }
          });
        }
      });
    },
    // 表单提交
    dataFormSubmit () {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(
              `/exam/exam/${!this.dataForm.id ? 'save' : 'update'}`
            ),
            method: 'post',
            data: this.$http.adornData({
              id: this.dataForm.id || undefined,
              examName: this.dataForm.examName,
              examCount: this.dataForm.examCount,
              examSum: this.dataForm.examSum,
              examHover: this.dataForm.examHover,
              teacherName: this.dataForm.teacherName,
              startTime: this.dataForm.startTime,
              endTime: this.dataForm.endTime,
              examTime: this.dataForm.examTime,
              createTime: this.dataForm.createTime,
              updateTime: this.dataForm.updateTime,
              isDelete: this.dataForm.isDelete
            })
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.visible = false;
                  this.$emit('refreshDataList');
                }
              });
            } else {
              this.$message.error(data.msg);
            }
          });
        }
      });
    },
    calculateExamTime() {
      const start = moment(this.dataForm.startTime);
      const end = moment(this.dataForm.endTime);
      const duration = moment.duration(end.diff(start));
      const hours = duration.asHours();
      this.dataForm.examTime = `${hours.toFixed(2)}小时`;
    },
  }
};
</script>
